//
//  ORMDb.h
//  storeapp
//
//  Created by huangyi on 13-4-25.
//  Copyright (c) 2013年 ios. All rights reserved.
//

#import <Foundation/Foundation.h>

@class ORMObject;

@interface ORMDatabase : NSObject

-(id)initWithDatabase:(NSString*)dbfile;
-(BOOL)open;
-(BOOL)close;

-(NSArray*)getSchema;
-(NSArray*)listOfTables;
-(NSArray*)getTableSchema:(NSString*)table;
-(NSArray*)listOfFields:(NSString*)table;

-(int)count:(NSString*)table;

-(ORMObject*)dispense:(NSString*)table;
-(ORMObject*)load:(NSString*)table rowid:(int)rowid;
-(ORMObject*)dispense:(NSString *)table ifNotExist:(int)rowid;

-(ORMObject*)findOne:(NSString*)table match:(NSString*)where,... NS_REQUIRES_NIL_TERMINATION;
-(NSArray*)findAll:(NSString*)table match:(NSString*)where,... NS_REQUIRES_NIL_TERMINATION;

-(int)store:(ORMObject*)object;
-(BOOL)trash:(ORMObject*)object;
-(NSDictionary*)dump:(ORMObject*)object;

-(BOOL)wipe:(NSString*)table;
-(BOOL)optimize;

@end


/*
 USAGE:

 ORMDatabase *db=[[ORMDatabase alloc] initWithDatabase:@"app.db".docPath];
 
 ORMObject *obj=[db dispense:@"bbb"];
 obj[@"zzzz"]=@"zzzz";
 [obj store];
 
 ORMObject *vv=[db load:@"news" rowid:1];
 vv[@"title"]=@"fffff";
 [vv store];
 
*/